package com.bo.lock.reentrantLock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: gpb
 * @Date: 2024/9/3 21:46
 * @Description:
 */
public class ICondition {
    public static void main(String[] args) throws InterruptedException {
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        new Thread(() -> {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + " await ~ ");
            try {
                condition.await();
                Thread.sleep(8000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName() + " signal ~");
        }, "t1").start();
        new Thread(() -> {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + " await ~ ");
            try {
                condition.await();
                Thread.sleep(20000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName() + " signal ~");
        }, "t2").start();
        new Thread(() -> {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + " await ~ ");
            try {
                condition.await();
                Thread.sleep(30000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName() + " signal ~");
        }, "t3").start();
        lock.lock();
        Thread.sleep(3000);
        condition.signal();
        condition.signalAll();
        condition.awaitNanos(30000);
        lock.unlock();
    }
}
